﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;

namespace PPAP.Web.SearchReport
{
    public partial class SampleCardSearch : PageBase
    {
        SamplecardBO sbo = new SamplecardBO();
        TestDepartmentBO tbo = new TestDepartmentBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
                btnSubmit_Click(null, null);
            }
        }

        private void LoadData()
        {
            DataSet dstest = tbo.GetList("");
            ddlDepartment.Items.Add(new ListItem("All", "All"));
            foreach (DataRow dr in dstest.Tables[0].Rows)
            {
                ddlDepartment.Items.Add(new ListItem(dr["TESTITEMNAME"].ToString(), dr["TESTITEMNAME"].ToString()));
            }
            DataSet dsUser = tbo.GetUserForTest();
            ddlDept.Items.Add(new ListItem("All", "All"));
            foreach (DataRow dr in dsUser.Tables[0].Rows)
            {
                ddlDept.Items.Add(new ListItem(dr["UserName"].ToString(), dr["UserName"].ToString()));
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string sql = "  ";
            if (txtPartNo.Text.Trim() != "")
                sql += " and r.bdntpartno='" + txtPartNo.Text.Trim() + "' ";
            if (txtSupplierNo.Text.Trim() != "")
                sql += " and l.suppliercode='" + txtSupplierNo.Text.Trim() + "' ";
            if (txtCreateStart.Text.Trim() != "")
                sql += " and trunc(starttime)>=to_date( '" + txtCreateStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtCreateEnd.Text.Trim() != "")
                sql += " and trunc(starttime)<=to_date( '" + txtCreateEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtTimeStart.Text.Trim() != "")
                sql += " and trunc(endtimeplan)>=to_date( '" + txtTimeStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtTimeEnd.Text.Trim() != "")
                sql += " and trunc(endtimeplan)<=to_date( '" + txtTimeEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (ddlDepartment.SelectedValue != "All")
                sql += " and t.testitemname='"+ddlDepartment.SelectedValue+"' ";
            if (ddlDept.SelectedValue != "All")
                sql += " and s.deptrep='"+ddlDept.SelectedValue+"' ";
            if (ddlStatus.SelectedValue != "All")
                sql += " and s.status="+ddlStatus.SelectedValue+" ";
            if (ddlResult.SelectedValue != "All")
                sql += " and s.result='"+ddlResult.SelectedValue+"' ";
            if (cbCase.Checked)
                sql += " and s.deptrep='" + CurrentUser.Id + "' ";
            sql += " and s.status!=9 ";
            sql += " and a.status!=9 ";
            DataSet ds = sbo.GetSampleForSearch(sql);
            AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
            dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            dgList.DataBind();
        }

        public string Status(string status)
        {
            string font = "";
            switch (status)
            {
                case "0":
                    font = "Created";
                    break;
                case "1":
                    font = "Circulating";
                    break;
                case "2":
                    font = "Circulating(<font color='red'>Reject</font>)";
                    break;
                default:
                    font = "Decided";
                    break;
            }
            return font;

        }

        

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            btnSubmit_Click(null, null);
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string sql = "  ";
            if (txtPartNo.Text.Trim() != "")
                sql += " and r.bdntpartno='" + txtPartNo.Text.Trim() + "' ";
            if (txtSupplierNo.Text.Trim() != "")
                sql += " and l.suppliercode='" + txtSupplierNo.Text.Trim() + "' ";
            if (txtCreateStart.Text.Trim() != "")
                sql += " and trunc(starttime)>=to_date( '" + txtCreateStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtCreateEnd.Text.Trim() != "")
                sql += " and trunc(starttime)<=to_date( '" + txtCreateEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtTimeStart.Text.Trim() != "")
                sql += " and trunc(endtimeplan)>=to_date( '" + txtTimeStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtTimeEnd.Text.Trim() != "")
                sql += " and trunc(endtimeplan)<=to_date( '" + txtTimeEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (ddlDepartment.SelectedValue != "All")
                sql += " and t.testitemname='" + ddlDepartment.SelectedValue + "' ";
            if (ddlDept.SelectedValue != "All")
                sql += " and s.deptrep='" + ddlDept.SelectedValue + "' ";
            if (ddlStatus.SelectedValue != "All")
                sql += " and s.status=" + ddlStatus.SelectedValue + " ";
            if (ddlResult.SelectedValue != "All")
                sql += " and s.result='" + ddlResult.SelectedValue + "' ";
            if (cbCase.Checked)
                sql += " and s.deptrep='" + CurrentUser.Id + "' ";
            sql += " and s.status!=9 ";
            sql += " and a.status!=9 ";
            DataSet ds = sbo.GetSampleForSearch(sql);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                r["ShowStatus"] = Status(r["status"].ToString());
                r["d1"] = r["starttime"].ToString() == "" ? r["starttime"] : DateTime.Parse(r["starttime"].ToString()).ToString("yyyy-MM-dd");
                r["d2"] = r["endtimeplan"].ToString() == "" ? r["endtimeplan"] : DateTime.Parse(r["endtimeplan"].ToString()).ToString("yyyy-MM-dd");
            }
            ds.Tables[0].Columns.Remove("STATUS");
            ds.Tables[0].Columns.Remove("ID");
            ds.Tables[0].Columns.Remove("STARTTIME");
            ds.Tables[0].Columns.Remove("ENDTIMEPLAN");
            ds.Tables[0].Columns.Remove("rid");

            ds.Tables[0].Columns["ROWNUM"].ColumnName = "No.";
            ds.Tables[0].Columns["PPARNO"].ColumnName = "PPAR.No";
            ds.Tables[0].Columns["BDNTPARTNO"].ColumnName = "Part.No";
            ds.Tables[0].Columns["SUPPLIERCODE"].ColumnName = "Supplier Code";
            ds.Tables[0].Columns["SAMPLECARDNO"].ColumnName = "Sample card No.";
            ds.Tables[0].Columns["TESTITEMNAME"].ColumnName = "Department";
            ds.Tables[0].Columns["RESULT"].ColumnName = "Result";
            ds.Tables[0].Columns["d1"].ColumnName = "Start date";
            ds.Tables[0].Columns["d2"].ColumnName = "End date";
            ds.Tables[0].Columns["SHOWSTATUS"].ColumnName = "Status";
            ds.Tables[0].Columns["DEPTREP"].ColumnName = "Dept.rep";
            CreateExcel(ds, "SampleCard");
        }
    }
}